package com.zmn.plat.services.impl.part;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Objects;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.pager.LayuiPageUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.model.entity.part.PartCategoryMap;
import com.zmn.plat.model.entity.part.PartCategoryMapQuery;
import com.zmn.plat.model.vo.part.PartCategoryMapVO;
import com.zmn.plat.persistence.interfaces.part.PartCategoryMapDAO;
import com.zmn.plat.services.interfaces.part.PartCategoryMapService;

/**
 * @author nowind
 * @since 17/06/12 12:22
 */
@Service
public class PartCategoryMapServiceImpl implements PartCategoryMapService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PartCategoryMapDAO partCategoryMapDAO;

    /**
     * 描述:通过 ID 查询 PartCategoryMapVO
     *
     * @param mapId
     *            ID
     * @return PartSkuPrice
     * @author DaiZhichao
     * @since 2018/10/23 16:33
     */
    @Override
    public PartCategoryMapVO findByKey(Integer mapId) {
        return partCategoryMapDAO.findVOByKey(mapId);
    }

    /**
     * 描述:通过 query 分页查询 PartCategoryMapVO
     *
     * @param query
     *            查询对象
     * @return List<PartSkuPrice>
     * @author DaiZhichao
     * @since 2018/10/23 16:34
     */
    @Override
    public List<PartCategoryMapVO> listVOPageByQuery(PartCategoryMapQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : partCategoryMapDAO.listVOPageByQuery(query);
    }

    /**
     * 描述:通过 query 查询 count
     *
     * @param query
     *            查询对象
     * @return Integer
     * @author DaiZhichao
     * @since 2018/10/23 16:36
     */
    @Override
    public Integer countByQuery(PartCategoryMapQuery query) {
        return partCategoryMapDAO.countByQuery(new LayuiPageUtil<>(query).setLayuiPageBlank().setStartEndTime().getQuery());
    }

    /**
     * 描述:通过 id 更新 PartCategoryMap
     *
     * @param entity
     *            PartCategoryMap
     * @param staff
     *            Staff
     * @return Integer
     * @author DaiZhichao
     * @since 2018/10/23 16:37
     */
    @Override
    public Integer updateByKey(PartCategoryMap entity, LoginStaff staff) {
        Date now = DateUtil.getNow();
        entity.setUpdater(staff.getRealName());
        entity.setUpdateTime(now);
        return partCategoryMapDAO.updateByKey(entity);
    }

    /**
     * 描述:新增 PartCategoryMap
     *
     * @param entity
     *            PartCategoryMap
     * @param staff
     *            Staff
     * @return Integer
     * @author DaiZhichao
     * @since 2018/10/23 16:37
     */
    @Override
    public Integer insert(PartCategoryMap entity, LoginStaff staff) {
        Date now = DateUtil.getNow();
        entity.setUpdater(staff.getRealName());
        entity.setCreater(staff.getRealName());
        entity.setUpdateTime(now);
        entity.setCreateTime(now);
        return partCategoryMapDAO.insert(entity);
    }

    /**
     * 描述:删除 PartCategoryMap
     *
     * @param mapId
     *            ID
     * @return Integer
     * @author DaiZhichao
     * @since 2018/10/23 16:37
     */
    @Override
    public Integer deleteByKey(Integer mapId) {
        return partCategoryMapDAO.deleteByKey(mapId);
    }

    /**
     * 描述:批量删除 PartCategoryMap
     *
     * @param mapIdList
     *            List<Integer>
     * @return Integer
     * @author DaiZhichao
     * @since 2018/10/23 16:37
     */
    @Override
    public Integer deleteByKeyBatch(List<Integer> mapIdList) {

        return partCategoryMapDAO.deleteByKeyBatch(mapIdList);
    }

    @Override
    public boolean findIdenticalMap(PartCategoryMap partCategoryMap) {
        List<PartCategoryMapVO> allMap = partCategoryMapDAO.listVOPageByQuery(new PartCategoryMapQuery());

        return allMap.stream().filter(map -> !Objects.equal(partCategoryMap.getMapId(), map.getMapId())).filter(map -> Objects.equal(partCategoryMap.getBaseCategId(), map.getBaseCategId()))
            .anyMatch(map -> Objects.equal(partCategoryMap.getCategId(), map.getCategId()));
    }
}
